package org.example.dao;

import org.example.pojo.Admin;
import org.example.utils.JDBCUtils;

import java.sql.*;

public class AdminDAO {

    // 注册管理员
    public boolean register(String username, String password) throws SQLException {
        String sql = "INSERT INTO Admin (username, password) VALUES (?, ?)";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, username);
            ps.setString(2, password);
            return ps.executeUpdate() > 0;
        }
    }

    // 登录管理员
    public Admin login(String username, String password) throws SQLException {
        String sql = "SELECT * FROM Admin WHERE username = ? AND password = ?";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, username);
            ps.setString(2, password);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    return new Admin(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
                } else {
                    return null;
                }
            }
        }
    }
}
